define(['jquery', 'bootstrap', 'backend', 'table', 'form','summernote'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'web_policy/index' + location.search,
                    add_url: 'web_policy/add',
                    edit_url: 'web_policy/edit',
                    del_url: 'web_policy/del',
                    multi_url: 'web_policy/multi',
                    table: 'web_policy',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                searchFormVisible:true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __(' id'),operate:false},
                        {field: 'tpic_image', title: __('Tpic_image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate:false},
                        {field: 'title', title: __('标题')},
                        // {field: 'content', title: __('Content')},
                        {field: 'online_time', title: __('Online_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,operate:false},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'),operate:false},
                        // {field: 'is_showswitch', title: __('是否前端展示'), searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle},
                        {field: 'state', title: __('分类'), searchList: {"1":__('资讯'),"2":__('政策法规'),"3":__('公告')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'web_policy/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'web_policy/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'web_policy/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
             //设置编辑器功能
            $.each(['c-content'],function (index,value) {
                console.log(value)
                var field_ids = '#'+value;
                $(field_ids).summernote({
                    minHeight: 300, //宽高，根据自己的需求设置：如果是手机上显示，就把宽设小一点，比如410px
                    maxwidth: 1000,
                    minwidth: 200,
                    maxHeight: 500,
                    lang:'zh-CN',
                    focus: false,
                    fontNames: [
                        'Arial', 'Arial Black', 'Serif', 'Sans', 'Courier',
                        'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
                        "Open Sans", "Hiragino Sans GB", "Microsoft YaHei",
                        '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆',
                    ],
                    fontNamesIgnoreCheck: [
                        "Open Sans", "Microsoft YaHei",
                        '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆'
                    ],
                    toolbar: [
                        ['style', ['style', 'undo', 'redo']],
                        ['font', ['bold', 'underline', 'strikethrough', 'clear']],
                        ['fontname', ['color', 'fontname', 'fontsize']],
                        ['para', ['ul', 'ol', 'paragraph', 'height']],
                        ['table', ['table', 'hr']],
                        ['insert', ['link', 'picture', 'video']],
                        ['select', ['image', 'attachment']],
                        ['view', ['fullscreen', 'codeview', 'help']],
                    ],
                    dialogsInBody: true,
                    followingToolbar: false,
                    callbacks: {
                        onImageUpload: function (files, editor) {
                            var $files = $(files);
                            // 通过each方法遍历每一个file
                            $files.each(function () {
                                var file = this;
                                // FormData，新的form表单封装，具体可百度，但其实用法很简单，如下
                                var data = new FormData();
                                // 将文件加入到file中，后端可获得到参数名为“file”
                                data.append("file", file);
                                // ajax上传
                                $.ajax({
                                    data: data,
                                    type: "POST",
                                    url: "/sfhitech/ajax/file",// div上的action
                                    cache: false,
                                    contentType: false,
                                    processData: false,
                                    // 成功时调用方法，后端返回json数据
                                    success: function (response) {
                                        if (response.code !== 1) {
                                            alert("上传失败：" + response.message);
                                        } else {
                                            console.log(response.data.url);
                                            // 插入到summernote
                                            $(field_ids).summernote('insertImage', response.data.url);
                                        }
                                    },
                                });
                            })
                        }, onFileUpload: function (files) { //文件上传，默认没有，具体怎么加，请看下一篇
                            var $files = $(files);
                            // 通过each方法遍历每一个file
                            $files.each(function () {
                                var file = this;
                                // FormData，新的form表单封装，具体可百度，但其实用法很简单，如下
                                var data = new FormData();
                                // 将文件加入到file中，后端可获得到参数名为“file”
                                data.append("file", file);
                                // ajax上传
                                $.ajax({
                                    data: data,
                                    type: "POST",
                                    url: "/sfhitech/ajax/file",// div上的action
                                    cache: false,
                                    contentType: false,
                                    processData: false,
                                    // 成功时调用方法，后端返回json数据
                                    success: function (response) {
                                        //console.log(response)
                                        if (response.error != 1) {
                                            alert("上传失败：" + response.message);
                                        } else {
                                            $(".note-video-url").val(response.data.url);
                                            //Insert Video之前是输入线上视频url地址后去除禁用，现在是我们上传之后给Video URL输入框赋值后去除禁用
                                            $(".note-video-btn").removeAttr("disabled");
                                        }
                                    },
                                });
                            });
                        },
                        //粘贴出现重复图片bug
                        onPaste: function (ne) {

                            var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
                            //内容为空不做判断
                            if(!bufferText){
                                //    ne.preventDefault();
                                ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
                                // Firefox fix
                                document.execCommand("insertText", false, bufferText);
                            }
                        },
                    }

                });
            })
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});